<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class FeedHelper extends AppHelper
{
    function obtenerFeed($feed_url, $posts) 
    {
        $timeout = 10;
        $ch = curl_init();
        curl_setopt ($ch, CURLOPT_URL, $feed_url);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $file_contents = curl_exec($ch);
        curl_close($ch);
 
         if($file_contents != FALSE)
        {
               
            try
            {
               // $content = file_get_contents($feed_url);
                $x = new SimpleXmlElement($file_contents);

                echo "<ul class='feed'>";
                    $i = 0;
                    while ($i < $posts)
                    {
                            echo "<li class='feed'><a class='feed' href=\"" . $x->channel->item[$i]->link . "\" title=\"" . $x->channel->item[$i]->title . "\" target=\"_blank\">" . $x->channel->item[$i]->title . "</a></li>";
                            $i++;
                    }
                echo "</ul>";

            } 

            catch (Exception $e) 
            {
                echo "<p>El RSS no a sido encontrado</p>";
            }
        }
    }
    
    function descripcionFeed($feed_url, $posts) 
    {
        $timeout = 10;
        $ch = curl_init();
        curl_setopt ($ch, CURLOPT_URL, $feed_url);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $file_contents = curl_exec($ch);
        curl_close($ch);
        //return ($file_contents) ? $file_contents : FALSE;

        if($file_contents != FALSE)
        {
                try
                {
                   // $content = file_get_contents($feed_url);
                    $rss = new SimpleXmlElement($file_contents);

                    echo "<ul class='feed'>";
                        $i = 0;
                        while ($i < $posts)
                        {
                                echo "<li class='feed'><a class='feed' href=\"" . $rss->channel->item[$i]->link . "\" descripcion=\"" . $rss->channel->item[$i]->descripcion . "\" target=\"_blank\">" . $rss->channel->item[$i]->description . "</a></li>";
                                $i++;
                        }
                    echo "</ul>";

                } 

                catch (Exception $e) 
                {
                    echo "<p>El RSS no a sido encontrado</p>";
                }
          }      
    }
}


?>
